package org.shoukaiseki.supersaiyan.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageSerializable;
import org.shoukaiseki.supersaiyan.common.annotation.Log;
import org.shoukaiseki.supersaiyan.common.core.controller.BaseController;
import org.shoukaiseki.supersaiyan.common.core.domain.base.AjaxResult;
import org.shoukaiseki.supersaiyan.common.core.page.TableDataInfo;
import org.shoukaiseki.supersaiyan.common.enums.BusinessType;
import org.shoukaiseki.supersaiyan.common.utils.StringUtils;
import org.shoukaiseki.supersaiyan.common.utils.poi.ExcelUtil;
import org.shoukaiseki.supersaiyan.system.domain.qo.LookupDictDataQO;
import org.shoukaiseki.supersaiyan.system.service.ISysDictDataService;
import org.shoukaiseki.supersaiyan.system.service.ISysDictTypeService;
import org.shoukaiseki.supersaiyan.web.model.qo.MultipleDictTypeQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.shoukaiseki.supersaiyan.common.core.domain.SysDictData;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @PostMapping(value = "/lookupDictData")
    public AjaxResult lookupDictData(@RequestBody LookupDictDataQO qo)
    {
        List<SysDictData> data = dictDataList(qo);
        return AjaxResult.successData(PageSerializable.of(data));
    }

    /**
     * 查询多个字典
     * @param paramList
     * @return
     */
    @PostMapping(value = "/type/multipleDictType")
    public AjaxResult multipleDictType(@RequestBody List<MultipleDictTypeQO> paramList){
        JSONObject resultData=new JSONObject();
        int i=0;
        for (MultipleDictTypeQO qo : paramList) {
            i++;
            List<SysDictData> data = dictDataList(qo);
            String aliasName = qo.getAliasName();
            if(StringUtils.isEmpty(aliasName)){
                aliasName="r"+i;
            }
            resultData.put(aliasName,data);
        }
        return AjaxResult.successData(resultData);
    }

    public List<SysDictData> dictDataList(LookupDictDataQO qo){
        String dictType = qo.getDictType();
        List<SysDictData> data;
        if (StringUtils.isEmpty(dictType))
        {
            data = new ArrayList<SysDictData>();
        }else {
            data = dictTypeService.selectDictDataByType(dictType);
        }
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        if(qo.isUnshiftAllOption()){
            SysDictData dd = new SysDictData();
            dd.setDictValue("");
            dd.setDictLabel("全部");
            data.add(0, dd);
        }
        return data;
    }

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType, LookupDictDataQO qo)
    {
        qo.setDictType(dictType);
        List<SysDictData> data = dictDataList(qo);
        return AjaxResult.success(data);
    }


    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateUserId(getUserId());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateUserId(getUserId());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
